package com.wondersgroup.app.util;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 *
 * @author wanglei
 */
public class VisitMethodAdvice implements MethodBeforeAdvice {

    private static final Logger logger = LoggerFactory.getLogger(VisitMethodAdvice.class);

    /*@Autowired
    private AccessHistoryService accessHistoryService;*/

    @Override
    public void before(Method method, Object[] args, Object targetClassInstance) throws Throwable {
        logger.debug("~~~~~~~~ enter VisitMethodAdvice ~~~~~~~~");
        long begin = System.currentTimeMillis();
        try {
            ServletRequestAttributes atts = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
            if (atts != null) {
                String functionName = null;
                String methodName = method.getName();
                
                HttpServletRequest req = atts.getRequest();
                String reqURL = req.getRequestURI();
                String serverHost = req.getLocalName();
                String serverAddress = req.getLocalAddr();
                if(reqURL!=null && !reqURL.isEmpty()){
                    int beginIndex = (reqURL.indexOf("/")==0)?1:0; //如果第一个字符是/则略过开头
                    if(reqURL.indexOf("/", beginIndex)>-1){
                        beginIndex = reqURL.indexOf("/", beginIndex) + 1;
                    }
                    int endIndex = reqURL.length();
                    if(reqURL.indexOf("/", beginIndex)>-1){
                        endIndex = reqURL.indexOf("/", beginIndex);
                    }
                    functionName = reqURL.substring(beginIndex, endIndex);
                }
                
                String deviceSn = req.getHeader("deviceSn");
                String appVersion = req.getHeader("appVersion");
                
                Integer loginUserId = null;
                String loginUserName = null;
                HttpSession session = req.getSession(true);
                /*AppLoginDevice appLoginDevice = (AppLoginDevice) session.getAttribute("appLoginDevice");
                if(appLoginDevice!=null){
                    loginUserId = appLoginDevice.getLoginUserId();
                }
                if (loginUserId != null) {
                    AppLoginUser loginUser = (AppLoginUser)session.getAttribute("userInfo");
                    if (loginUser != null && loginUser.getEmp() != null) {
                        loginUserName = loginUser.getEmp().getPersonName();
                    }
                }*/
                
                /*if(deviceSn!=null && !deviceSn.isEmpty()){
                	MgrUserVisitHistory visitHistory = new MgrUserVisitHistory();
                    visitHistory.setClientAppVersion(new BigDecimal(appVersion));
                    visitHistory.setClientDeviceSn(deviceSn);
                    visitHistory.setClientUserId(loginUserId);
                    visitHistory.setClientUserName(loginUserName);
                    visitHistory.setServerHostName(serverHost);
                    visitHistory.setServerIpAddress(serverAddress);
                    visitHistory.setFunctionName(functionName); //功能名称
                    visitHistory.setRequestName(methodName);
                    visitHistory.setInitialRequest(reqURL); //请求地址
                    visitHistory.setCreateDate(new Date());
                    
                    //accessHistoryService.createMgrUserVisitHistory(visitHistory);
                }*/
            }
        } catch (Exception e) {
            logger.error(null, e);
        } finally {
            long useTime = System.currentTimeMillis() - begin;
            logger.debug("~~~~~~~~ VisitMethodAdvice useTime:"+useTime+"~~~~~~~~");
            logger.debug("~~~~~~~~ leave VisitMethodAdvice ~~~~~~~~");
        }
    }

}
